package com.mihoyo.org.controller;

import com.mihoyo.basic.utils.AjaxResult;
import com.mihoyo.basic.utils.PageList;
import com.mihoyo.org.domain.Employee;
import com.mihoyo.org.query.EmpQuery;
import com.mihoyo.org.service.IEmpService;
import com.mihoyo.org.service.impl.EmpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/emp")
@RestController
public class EmpController {

    @Autowired
    private IEmpService empService;

    @GetMapping
    public List<Employee> findAll () {
        return empService.findAll();
    }
    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id) {
        return empService.findById(id);
    }

    @PostMapping
    public PageList<Employee> queryByPage(@RequestBody EmpQuery empQuery) {
        return  empService.queryByPage(empQuery);
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        if (employee.getId() == null) {
            empService.add(employee);
        } else {
            empService.update(employee);
        }
        return AjaxResult.me().success();
    }

    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        empService.delete(id);
        return AjaxResult.me().success();
    }

    @PatchMapping
    public AjaxResult batchEmp(List<Long> ids) {
        empService.batchDelete(ids);
        return AjaxResult.me().success();
    }

    @GetMapping("/findEmps/{id}")
    public List<Employee> findEmps(@PathVariable("id") Long id) {
        return empService.findEmps(id);
    }

}
